import { FairyGUI } from 'csharp';
import FairyUtils from '../com/utils/FairyUtils';
import GameLayer from './GameLayer';
import LayerType from './LayerType';
import ViewStruct from './ViewStruct';

export default class UIBaseWindow extends FairyGUI.GComponent{

    public cls: any = null;

    protected viewStruct: ViewStruct = null;
     /**包名 */
     protected _pkgName: string = "";
     /**类名 */
     protected _resName: string = "";

    protected panel:FairyGUI.GComponent;
    protected btns:Array<FairyGUI.GButton> = [];
    protected btn_close:FairyGUI.GButton;

    constructor( pkgName:string , resName:string ){

        super();

        this._pkgName = pkgName;
        this._resName = resName;

        this.panel = FairyGUI.UIPackage.CreateObject( pkgName , resName ).asCom;
        // console.log( "------>pkgName: " + pkgName + " resName: "+resName );
        this.AddChild( this.panel );

        // let pkg:FairyGUI.UIPackage = FairyGUI.UIPackage.GetByName( pkgName );
        // if( pkg ){
        //     console.log( "------>pkg.name: " + pkg.name + " pkg.id: "+pkg.id + " pkg.customId: "+pkg.customId);
        //     let items = pkg.GetItems();
        //     if( items ){
        //         console.log( "------>items.Count : "  + items.Count );
        //         for(let i=0;i<items.Count;i++){
        //             let item = items.get_Item(i);
        //             console.log( "------>items.name : "  + item.name + " item.id: " + item.id );
        //         }
        //     }            
        // }

        this.SetViewStruct();

        FairyUtils.setVar( this.panel , this , this.btns );   
        this.registerClasses();
        this.initUI();        

        Object.defineProperty(this, "viewWidth", {
            get: function () { 
                return this.panel && this.panel.viewWidth;
            }
        });

        Object.defineProperty(this, "viewHeight", {
            get: function () { 
                return this.panel && this.panel.viewHeight;
            }
        });
    }

    /**设置视图结构 */
    protected SetViewStruct(): void {
        if (this.viewStruct == null) {
            this.viewStruct = new ViewStruct();
        }
    }

    public GetViewStruct():ViewStruct{

        return this.viewStruct;
    }

    public get panelName():string{

        return this._resName;
    }

    public initUI():void{
        
    }

    protected registerClasses(): void {

        // PanelRegister.registerClass( this._panelVo.pkgName );
    }

    public addAllListener():void{


    }

    public removeListener():void{

        
    }

    public show( data:any ):void{

        let btn:FairyGUI.GButton;
        for( btn of this.btns ){
            if(!btn) continue;
            let callback1 = (context:FairyGUI.EventContext)=>{
                this.onBtnClick( context.sender as FairyGUI.GButton );
            }
            btn && btn.onClick.Clear();
            btn && btn.onClick.Add( callback1 );
            this[btn.name + "_EventCallBack"] = callback1;
        }

        this.onResize();
    }

    protected onBtnClick( btn:FairyGUI.GButton ):void{

        console.log( "------->onBtnClick: " + (btn?btn.name:btn) );
        if( btn == this.btn_close ){            
            this.hide();
        }
    }

    Reset():void{


    }

    onResize():void{

        LayerType.SetWindowLayout(this);
    }

    public hide():void{

        let btn:FairyGUI.GButton;
        for( btn of this.btns ){
            let callback1 = this[btn.name + "_EventCallBack"];
            btn && callback1 && btn.onClick.Remove( callback1 );
        }
        this.RemoveFromParent();
    }
}